package com.foreknow.dao.inpl;

import com.foreknow.dao.AdminDao;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Admin;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.jar.JarOutputStream;

public class AdminDaoImpl extends BaseDao implements AdminDao {

    //登录操作
    @Override
    public Admin isLogin(String username, String password) {
        //1. 链接数据库
//        DBManager dbManager= DBManager.getInstance();
//        Connection conn =dbManager.getConnection();
        //2. 调用模板中JdbcTemplate中的方法query方法进行查询
//        JdbcTemplate jt =JdbcTemplate.getInstance();
        //一定要调用JdbcTemplate的setConnection方法才能连接上数据库
//        jt.setConnection(conn);
        //3. 获取工厂类的队象.然后才能调用getMap方法根据key获取value值
//        MappingFactory ma = MappingFactory.getInstance();
        String sql="select * from admin where username=? and password=?";
        EntityMapping mapping =mappingFactory.getMap("ADMIN_MAPPINGIMPL");//获取到AdminMapper对象
       List<Object> list= jdbcTemplate.query(sql,mapping,username,password);
        //3. 返回结果
       if (list.size()>0){
            return (Admin) list.get(0);
       }else {
           return null;

       }

    }

    @Override
    public boolean addAdmin(Admin admin) throws SQLException {
        String sql ="insert into admin (username,password) values (?,?)";
        int isRight =jdbcTemplate.update(sql,admin.getUsername(),admin.getPassword());
        return isRight==1;
    }


}
